<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Markdown Editor</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: Arial, sans-serif;
        }

        #markdown {
            width: 50%;
            height: 80vh;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
            padding: 10px;
            outline: none;
        }

        #preview {
            width: 50%;
            height: 80vh;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
            padding: 10px;
            overflow-y: scroll;
        }

        #submit {
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            padding: 10px 20px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin-top: 10px;
            cursor: pointer;
        }

        #submit:hover {
            background-color: #3e8e41;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/showdown/dist/showdown.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/showdown/dist/github.min.css">
</head>
<body>
<textarea id="markdown"></textarea>
<div id="preview"></div>
<button id="submit">提交</button>

<script>
    var markdown = document.querySelector('#markdown');
    var preview = document.querySelector('#preview');
    var submit = document.querySelector('#submit');

    var converter = new showdown.Converter();

    markdown.addEventListener('input', function () {
        var markdownText = markdown.value;
        var htmlText = converter.makeHtml(markdownText);
        preview.innerHTML = htmlText;
    });

    submit.addEventListener('click', function () {
        var markdownText = markdown.value;
        var htmlText = converter.makeHtml(markdownText);

        var xhr = new XMLHttpRequest();
        xhr.open('POST', 'http://127.0.0.1:8000/news');
        xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
        xhr.onreadystatechange = function () {
            if (xhr.readyState === XMLHttpRequest.DONE) {
                if (xhr.status === 200) {
                    console.log(xhr.responseText);
                } else {
                    console.log('Error sending request!');
                }
            }
        };
        xhr.send(JSON.stringify({
            content: htmlText,
            news_type: "交流讨论",
            title: "tlp2",
            title_img: "aasdf",
            course_id: 1
        }));
    });
</script>
</body>
</html>